package rad.framework.mda.actions;

import rad.framework.mda.util.ModelUtil;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.uml2.uml.Model;

/**
 * @author boudyacho
 * 
 */
public class ReloadAction extends Action {

	private IStructuredSelection selection;

	private TreeViewer viewer;

	/**
	 * @param viewer
	 * @param selection
	 */
	public ReloadAction(TreeViewer viewer, ISelection selection) {
		this.selection = (IStructuredSelection) selection;
		setText("Reload Model");
		this.viewer = viewer;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run() {
		Model model = (Model) selection.getFirstElement();
		Resource resource = model.eResource();
		if (resource.isLoaded()) {
			resource.unload();
			ModelUtil.getEditingDomain().getResourceSet().getResources().remove(resource);
			ModelUtil.getEditingDomain().loadResource(resource.getURI().toFileString());
			viewer.refresh();
		}
	}
}
